import { Client } from "./mod.ts";
import { User } from "./user.ts";
import { MediaTypes } from "./utils.ts";

/**
 * CommitMeta contains meta information of a commit in terms of API.
 */
export interface CommitMeta {
  url: string;
  sha: string;
}

/**
 * CommitUser contains information of a user in the context of a commit.
 */
export interface CommitUser {
  name: string;
  email: string;
  date: string;
}

/**
 * RepoCommit contains information of a commit in the context of a repository.
 */
export interface RepoCommit {
  url: string;
  author: CommitUser;
  committer: CommitUser;
  message: string;
  tree: CommitMeta;
}

/**
 * Commit contains information generated from a Git commit.
 */
export interface Commit extends CommitMeta {
  html_url: string;
  commit: RepoCommit;
  author: User;
  committer: User;
  parents: CommitMeta[];
}

/**
 * Repository Commit API endpoints
 */
export class CommitAPI {
  constructor(private client: Client) {}

  /**
   * Get a single commit
   */
  async getCommit(owner: string, repo: string, commitID: string): Promise<Commit> {
    return await this.client.doRequest<Commit>(
      "GET", 
      `/repos/${owner}/${repo}/commits/${commitID}`
    );
  }

  /**
   * Get the SHA of a reference (branch, tag, or commit)
   */
  async getReferenceSHA(owner: string, repo: string, ref: string): Promise<string> {
    return await this.client.doRequest<string>(
      "GET", 
      `/repos/${owner}/${repo}/commits/${ref}`, 
      { "Accept": MediaTypes.MediaApplicationSHA }
    );
  }
} 